package com.codechuan.demo.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.floatWindowManager.FloatWindowManager;
import com.codechuan.demo.ui.home.HomeFragment;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;
import com.example.libbaiduselectlocation.BDLocationActivity;
import com.example.libbaiduselectlocation.bean.SelectLocationBDBean;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class LoginActivity extends BDLocationActivity {

    private NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // table切换启动
        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupWithNavController(navView, navController);
        FloatWindowManager.get().showToolView();

        Constant.context = this;


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == Activity.RESULT_OK) {
            SelectLocationBDBean bean = (SelectLocationBDBean) data.getSerializableExtra("data");
            String district = bean.getLocation().getDistrict();
            String address = bean.getLocation().getAddress();
            HomeFragment.updateStartAddressText(district + address);
            Constant.START_ADDRESS = district + address;
            DialogUtil.showDialog(Constant.context, Constant.START_ADDRESS);
            // 开始定位信息
            new Thread(() -> {
                Constant.cityInfo = HttpControllerUtil.getCityInfo(Constant.START_ADDRESS);
                if (Constant.cityInfo != null) {
                    Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "定位成功."));
                } else {
                    Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "定位失败."));
                }
            }).start();
        }

    }

    @Override
    public void onLocation(String province, String city, String area, String featureName, String addressLine) {
        if (Constant.SS_DW_INFO.equals(featureName.substring(2)))
            return;
        if (featureName == null || featureName.isEmpty()) {
            Constant.handler.post(() -> {
                Constant.START_ADDRESS = "请使用模拟定位~";
            });
        }
        Constant.SS_DW_INFO = featureName.substring(2);
        // HomeFragment.updateStartAddress( featureName.substring(2));
    }

    @Override
    public void onLocationFail(String s) {
        System.out.println("s = " + s);
    }


}